﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Practice1.Model;

namespace Practice1.ViewModel.MvvmPractice
{
    public class AdvCommandParamViewModel: ViewModelBase
    {
        public AdvCommandParamViewModel()
        {
            ResType = new ResType()
            {
                List = new List<KeyValuePair<string, string>>()
                {
                    new KeyValuePair<string, string>("0", "请选择..."),
                    new KeyValuePair<string, string>("1", "WinForm"),
                    new KeyValuePair<string, string>("2", "WPF"),
                    new KeyValuePair<string, string>("3", "Xamarin"),
                }
            };
        }

		private UserParam userParam;

		public UserParam UserParam
		{
			get { return userParam; }
            set
            {
                userParam = value;
                RaisePropertyChanged(nameof(UserParam));
            }
		}

        private string argStrTo;

        public string ArgStrTo
        {
            get { return argStrTo; }
            set
            {
                argStrTo = value;
                //RaisePropertyChanged(nameof(ArgStrTo));
                RaisePropertyChanged(() => ArgStrTo);
            }
        }

        private string filePath;

        public string FilePath
        {
            get { return filePath; }
            set
            {
                filePath = value;
                RaisePropertyChanged(nameof(FilePath));
            }
        }

        #region 传递单个参数
        private RelayCommand<string> passArgStrCommand;
        public RelayCommand<string> PassArgStrCommand
        {
            get
            {
                if (passArgStrCommand == null)
                {
                    passArgStrCommand = new RelayCommand<string>(p => ExecutePassArgStr(p));
                }

                return passArgStrCommand;
            }
            set { passArgStrCommand = value; }
        }

        private void ExecutePassArgStr(string arg)
        {
            ArgStrTo = arg;
        }
        #endregion

        #region 传递多个参数 静态/动态绑定
        private RelayCommand<UserParam> passArgsCommand;

        public RelayCommand<UserParam> PassArgsCommand
        {
            get
            {
                if (passArgsCommand == null)
                {
                    passArgsCommand = new RelayCommand<UserParam>(p => ExecutePassArgs(p));
                }

                return passArgsCommand;
            }
            set { passArgsCommand = value; }
        }

        private void ExecutePassArgs(UserParam param)
        {
            UserParam = param;
        }
        #endregion

        #region 拖拽命令 Drop事件转DropCommand
        private RelayCommand<DragEventArgs> dropCommand;

        public RelayCommand<DragEventArgs> DropCommnad
        {
            get
            {
                if (dropCommand == null)
                {
                    dropCommand = new RelayCommand<DragEventArgs>(arg => ExecuteDrop(arg));
                }
                return dropCommand;
            }
            set { dropCommand = value; }
        }

        private void ExecuteDrop(DragEventArgs e)
        {
            Array array = (Array) e.Data.GetData(DataFormats.FileDrop);
            FilePath = array.GetValue(0).ToString();
        }
        #endregion

        #region 事件转命令执行
        private string selectInfo;

        public string SelectInfo
        {
            get { return selectInfo; }
            set
            {
                selectInfo = value;
                RaisePropertyChanged(nameof(SelectInfo));
            }
        }

        private ResType resType;

        public ResType ResType
        {
            get { return resType; }
            set
            {
                resType = value;
                RaisePropertyChanged(nameof(ResType));
            }
        }

        private RelayCommand selectCommand;

        public RelayCommand SelectCommand
        {
            get
            {
                if (selectCommand == null)
                {
                    selectCommand = new RelayCommand(() => ExecuteSelect());
                }
                return selectCommand;
            }
            set { selectCommand = value; }
        }

        private void ExecuteSelect()
        {
            if (resType != null && resType.SelectIndex > 0)
            {
                SelectInfo = ResType.List[ResType.SelectIndex].Value;
            }
        } 
        #endregion
    }
}
